package edu.gatech.spacetraderteam3.model;

import java.util.ArrayList;
import java.util.List;

/**
 * Factory that spits out encounters.
 * @author Aaron
 * @version 22
 */
public final class EncounterFactory {
    /**
     * Maximum random value constant
     */
    private static final int MAXRANDVAL = 4;

    /**
     * Pirate enocunter
     */
    private static final int FIRSTCASE = 1;

    /**
     * trade encounter
     */
    private static final int SECONDCASE = 2;

    /**
     * police encounter
     */
    private static final int THIRDCASE = 3;

    /**
     * Generates a random encounter and returns it.
     * @return	Trade encounter, police, pirates, or No Encounter.
     */
    public static Encounter generateEncounter() {
        double randVal = Math.random();

        randVal *= MAXRANDVAL;
        final int type = (int) randVal;
        switch (type) {
            case FIRSTCASE:
                return pirateEncounter();
            case SECONDCASE:
                return tradeEncounter();
            case THIRDCASE:
                return policeEncounter();
            default:
                return noEncounter();
        }
    }

    /**
     * The defualt no encounter encounter. Only one option to be added to the list of
     * actions: okay
     * 
     * @return the empty encounter
     */
    private static Encounter noEncounter() {
        final List<EncounterAction> actions = new ArrayList<EncounterAction>();
        actions.add(EncounterAction.OKAY);
        return new Encounter("You encounter no one else.", actions, null);
    }

    /**
     * Pirate encounters! Adds letting pirates board, attacking, and running away to the
     * list of actions and returns the encounter
     * 
     * @return the pirate encounter
     */
    private static Encounter pirateEncounter() {
        final List<EncounterAction> actions = new ArrayList<EncounterAction>();
        actions.add(EncounterAction.LET_PIRATES_BOARD);
        actions.add(EncounterAction.ATTACK);
        actions.add(EncounterAction.FLEE);
        final Actor pirate = new Actor("Black Beard", Faction.PIRATE, new Gnat());
        pirate.populateInventory();
        return new Encounter("As you fly through space, you are stopped by a Pirate.",
                actions, pirate);
    }

    /**
     * Trade encounter with a merchant. Creates and arraylist of actions (trade and
     * no_trade) and returns the encounter
     * 
     * @return the trade encounter
     */
    private static Encounter tradeEncounter() {
        final List<EncounterAction> actions = new ArrayList<EncounterAction>();
        actions.add(EncounterAction.TRADE);
        actions.add(EncounterAction.NO_TRADE);
        final Actor trader = new Actor("The Jesus", Faction.MERCHANT, new Gnat());
        trader.populateInventory();
        return new Encounter(
                "As you fly through space, a fellow merchant approaches you to make a trade.",
                actions, trader);
    }

    /**
     * Police encounter. Adds actions to an arraylist ( Let them board, attack and flee)
     * and returns the encounter
     * 
     * @return the police encounter
     */
    private static Encounter policeEncounter() {
        final List<EncounterAction> actions = new ArrayList<EncounterAction>();
        actions.add(EncounterAction.LET_POLICE_BOARD);
        actions.add(EncounterAction.ATTACK);
        actions.add(EncounterAction.FLEE);
        final Actor police = new Actor("Da Po-Lice", Faction.POLICE, new Gnat());
        police.populateInventory();
        return new Encounter(
                "As you approach your destination, you are approached by the Police",
                actions, police);
    }
    
    /**
     * String representation of EncounterFactory. Should never be seen.
     * @return A foreboding warning...
     */
    public String toString(){
    	return "EncounterFactory. You shouldn't be seeing this...";
    }
}